package com.tianjipms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianjipms.entity.Building;
import com.tianjipms.entity.Repair;
import com.tianjipms.service.BuildingService;
import com.tianjipms.service.HouseService;
import com.tianjipms.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/building")
public class BuildingController {
    @Resource
    private BuildingService buildingService;
    @Resource
    private HouseService houseService;
    //增加楼栋
    @PostMapping("/add")
    public Result addBuilding(@RequestBody Building building){
        if(buildingService.existsByName(building.getName())){
            return Result.error().message("楼栋名字已存在");
        }
        return Result.ok(buildingService.save(building));
    }
    //分页查询楼栋
    @GetMapping("/listAllBuilding")
    public Result listByBuilding(){
        return Result.ok(buildingService.list());
    }
    //分页查询楼栋信息
    @GetMapping("/listAll")
    public Result listAll(@RequestParam(defaultValue = "1") Long pageNum,
                          @RequestParam(defaultValue = "5") Long pageSize){
        IPage<Building> page = new Page<>(pageNum, pageSize);
        buildingService.page(page);
        return Result.ok(page);
    }
    //删除楼栋
    @DeleteMapping("/delete/{id}")
    public Result deleteBuilding(@PathVariable Long id){
        if(houseService.existsByBuildingId(id)){
            return Result.error().message("该楼栋无法删除,该楼栋的楼房信息未删除");
        }
        buildingService.removeById(id);
        return Result.ok();
    }
}
